<?php
namespace Sys\Controller;
use Todo\Controller\CurdController;

class RoleController extends CurdController {

    /**
     * 角色授权菜单
     */
    public function menu(){
        $id = I('get.id/d');
        if(!$id) $this->error('非法请求!');

        $menuList = D('Todo/Sys/Menu')->where(array('status'=>1))->order('pid ASC,sort ASC')->select();
        $appList = D('Todo/Sys/Application')->where(array('status'=>1,'type'=>array('in', '1,2')))->order('sort ASC')->select();
        foreach ($appList as &$app){
            $app['module'] = array();
            $app['menu'] = array();
            $app['span'] = 1;
            foreach ($menuList as $menu){
                if($menu['app_id'] == $app['id']){
                    if($menu['pid'] == 0) {
                        if ($menu['type'] == 3) {
                            $app['module'][$menu['id']] = $menu;
                        } else{
                            $app['menu'][$menu['id']] = $menu;
                        }
                    }else{
                        $app['module'][$menu['pid']]['menu'][] = $menu;
                    }
                }
            }
            $app['span'] += count($app['module']);
        }
        $this->assign('appList', $appList);
        $menus = D('Todo/Sys/Menu')->query('SELECT * FROM sys_role_menu WHERE role_id = '. $id);
        $this->assign('menus', array_column($menus, 'menu_id'));
        $this->assign('id', $id);
        $this->display();
    }
}